import express, { json, urlencoded } from 'express'
import Mock from 'mockjs'

/**
 * Mock 接口
 * 1. 修改开发环境的HOST地址，配置为下方配置端口地址
 * 2. 修改完后执行npm run mock命令去运行mock服务
 * 3. mock文件通过nodemon进行监听，每当文件有所改变会自动重启服务(需等待端口启动好后才可访问到新内容)
 */

const app = express()
app.use(json())
app.use(urlencoded({ extended: false }))

// 登录 用户信息 首页接口
app.post('/lsu-dev/oauth/token', (req, res) => {
  console.log('🚀 ~ app.token ~ req:', req)
  res.json({
    code: 200,
    data: {
      token: 'backend-token-123456789-PPP',
      refreshToken: 'backend-refreshToken-123456789-PPP',
      data: { uid: 1 },
    },
    message: 'success',
  })
})
app.post('/lsu-dev/user/getUser', (req, res) => {
  console.log('🚀 ~ app.getUser ~ req:', req)
  res.json({
    code: 200,
    data: {
      data: {
        name: 'damon',
        userRole: 'ADMIN',
      },
    },
    message: 'success',
  })
})
app.post('/lsu-dev/core/cases/getAllCases', (req, res) => {
  res.json({
    code: 200,
    data: Mock.mock({
      name: '@cname',
      tableList: Array.from({ length: 6 }, () =>
        Mock.mock({
          'Id|1000000000000000008-1999999999999999999': 1,
          'name': '@cname',
          'date': '@date',
          'description': '@title(2)',
          'type|1': ['success', 'error', 'warning'], // 选一个
          'amount': '@integer(1000, 4000)', // 生成1000-4000的随机整数
          'image': Mock.Random.image('200x200', '#50B347', '#FFF', 'Mock.js'),
          'accountList|1-7': [ // 随机生成1-7个数组元素
            {
              name: 'Book Blance',
              price: '@integer(1000, 4000)',
            },
          ],
        })),
    }),
    message: 'success',
  })
})

// TODO接口

// 端口启动完成后(需要一点时间)再去请求接口
const PORT = 8082
app.listen(PORT, () => {
  console.log(`app is listening port ${PORT}`)
})
